<?php
/**
 * 订单模型
 * @author wtwei
 * @version $Id: OrderModel.class.php 1 2013-07-07 06:04:44Z wtwei1109@gmail.com $
 */
class OrderModel extends CommonModel {

	//自动验证
	protected $_validate = array(
		array('uid', 'checkUid', '用户账号不存在！', self::VALUE_VALIDATE, 'callback', self:: MODEL_BOTH),
		array('user', 'require', '收货人不能为空！', self::MUST_VALIDATE, 'regex', self:: MODEL_INSERT),
		array('user_tel', 'require', '收货电话不能为空！', self::MUST_VALIDATE, 'regex', self:: MODEL_INSERT),
		array('send_date', 'require', '送货日期不能为空！', self::MUST_VALIDATE, 'regex', self:: MODEL_INSERT),
		array('address', 'require', '收货地址不能为空！', self::MUST_VALIDATE, 'regex', self:: MODEL_INSERT),
		// array('invoice_type', 'require', '发票类型不能为空！', self::MUST_VALIDATE, 'regex', self:: MODEL_INSERT),
		// array('invoice_title', 'require', '发票抬头不能为空！', self::MUST_VALIDATE, 'regex', self:: MODEL_INSERT),
		// array('norm', 'require', '发票规格不能为空！', self::MUST_VALIDATE, 'regex', self:: MODEL_INSERT),
	);

	//自动完成
	protected $_auto = array(
		array('uid', 'getUid', self::MODEL_BOTH, 'callback'),
		array('order_sn', 'order_sn', self::MODEL_INSERT, 'callback'),
		array('create_time', 'time', self::MODEL_INSERT, 'function'),
		array('update_time', 'time', self::MODEL_BOTH, 'function'),
		array('send_date', 'strtotime', self::MODEL_BOTH, 'function'),
	);
	
	// 获取用户ID
	public function getUid() {
		if ($this->_data ['admin_type']) {
			return $this->_data ['uid'] ? $this->_data ['uid'] : 0;
		} else {
			$user = member_info ();
			return $user ['id'];
		}
	}
	
	// 检查用户
	public function checkUid() {
		$this->_data ['uid'] = M ( 'Member' )->where ( array (
				'email' => $this->_data ['uid'],
				'status' => 1 
		) )->getField ( 'id' );
		return $this->_data ['uid'] ? true : false;
	}
	
	// 生成订单号方法
	public function order_sn() {
		list ( $usec, $sec ) = explode ( " ", microtime () );
		$sn = date ( 'ymdHis', $sec ) . substr ( $usec, strpos ( $usec, '.' ) + 1, 3 );
		return $sn;
	}
	
	// 验证购物车商品
	protected function goods($value) {
		static $goods = array ();
		empty ( $value ) || $goods = $value;
		return empty ( $goods ) ? false : $goods;
	}
	

}

?>